/**
 * Copyright © 2016-2025 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.terracloud.server.dao.model.sql;

import com.terracloud.server.common.data.log.TerminalLog;
import com.terracloud.server.common.data.log.LogLevel;
import com.terracloud.server.common.data.log.LogType;
import com.terracloud.server.common.data.log.LogStatus;
import com.terracloud.server.common.data.id.TerminalLogId;
import com.terracloud.server.common.data.id.TenantId;
import com.terracloud.server.common.data.id.DeviceId;
import com.terracloud.server.dao.model.BaseSqlEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.UUID;

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "terminal_log")
public class TerminalLogEntity extends BaseSqlEntity<TerminalLog> {

    @Column(name = "tenant_id")
    private UUID tenantId;
    
    @Column(name = "device_id")
    private UUID deviceId;
    
    @Column(name = "device_name")
    private String deviceName;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "log_type")
    private LogType logType;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "log_level")
    private LogLevel logLevel;
    
    @Column(name = "content_summary")
    private String contentSummary;
    
    @Column(name = "content_details")
    private String contentDetails;
    
    @Column(name = "occur_time")
    private Long occurTime;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "process_status")
    private LogStatus processStatus;
    
    @Column(name = "processor")
    private String processor;
    
    @Column(name = "sla_status")
    private String slaStatus;
    
    @Column(name = "sla_deadline")
    private Long slaDeadline;
    
    @Column(name = "duration")
    private Long duration;
    
    @Column(name = "is_critical")
    private Boolean isCritical;

    public TerminalLogEntity() {
        super();
    }

    public TerminalLogEntity(TerminalLog terminalLog) {
        if (terminalLog.getId() != null) {
            this.setId(terminalLog.getId().getId());
        }
        if (terminalLog.getTenantId() != null) {
            this.tenantId = terminalLog.getTenantId().getId();
        }
        if (terminalLog.getDeviceId() != null) {
            this.deviceId = terminalLog.getDeviceId().getId();
        }
        this.deviceName = terminalLog.getDeviceName();
        this.logType = terminalLog.getLogType();
        this.logLevel = terminalLog.getLogLevel();
        this.contentSummary = terminalLog.getContentSummary();
        this.contentDetails = terminalLog.getContentDetails();
        this.occurTime = terminalLog.getOccurTime();
        this.processStatus = terminalLog.getProcessStatus();
        this.processor = terminalLog.getProcessor();
        this.slaStatus = terminalLog.getSlaStatus();
        this.slaDeadline = terminalLog.getSlaDeadline();
        this.duration = terminalLog.getDuration();
        this.isCritical = terminalLog.getIsCritical();
    }

    @Override
    public TerminalLog toData() {
        TerminalLog terminalLog = new TerminalLog(new TerminalLogId(this.getId()));
        terminalLog.setCreatedTime(this.getCreatedTime());
        if (this.tenantId != null) {
            terminalLog.setTenantId(new TenantId(this.tenantId));
        }
        if (this.deviceId != null) {
            terminalLog.setDeviceId(new DeviceId(this.deviceId));
        }
        terminalLog.setDeviceName(this.deviceName);
        terminalLog.setLogType(this.logType);
        terminalLog.setLogLevel(this.logLevel);
        terminalLog.setContentSummary(this.contentSummary);
        terminalLog.setContentDetails(this.contentDetails);
        terminalLog.setOccurTime(this.occurTime);
        terminalLog.setProcessStatus(this.processStatus);
        terminalLog.setProcessor(this.processor);
        terminalLog.setSlaStatus(this.slaStatus);
        terminalLog.setSlaDeadline(this.slaDeadline);
        terminalLog.setDuration(this.duration);
        terminalLog.setIsCritical(this.isCritical);
        return terminalLog;
    }
}